package com.resource.map
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import flashx.textLayout.formats.WhiteSpaceCollapse;
	
	import org.osmf.layout.AbsoluteLayoutFacet;
	
	public class BaseMap extends Sprite
	{
		private var _url:String;
		
		private var _w:int;
		private var _h:int;
		private var _tileWidth:int;
		private var _tileHeight:int;
		private var _stepWidth:int;
		private var _stepHeight:int;
		private var _mapWidth:int;
		private var _mapHeight:int;
		private var _backList:Array;
		private var _frontList:Array;
		private var _walkableData:ByteArray;
		private var _startX:int;
		private var _startY:int;
		
		private var _tiles:Array;
		private var _items:Array;
		
		private var _cX:int;
		private var _cY:int;
		
		private var _backLayer:Sprite = new Sprite;
		private var _frontLayer:Sprite = new Sprite;
		
		public function BaseMap(w:int = 3, h:int = 3)
		{
			addChild(_backLayer);
			addChild(_frontLayer);
			super();
		}
		
		public function init(_url:String, data:ByteArray, w:int = 3, h:int = 2):void{
			this._url = _url;
			
			var res:Object = MapDecoder.decode(data);
			
			_tileWidth = res.tileWidth;
			_tileHeight = res.tileHeight;
			_stepWidth = res.stepWidth;
			_stepHeight = res.stepHeight;
			_mapWidth = res.mapWidth;
			_mapHeight = res.mapHeight;
			_backList = res.backList;
			_frontList = res.frontList;
			_walkableData = res.walkableData;
			
			_cX = 0;
			_cY = 0;
			_w = w;
			_h = h;	
			
			_backList = [];
			_frontList = [];
			for(var i:int=0; i<=_mapWidth/_tileWidth; i++){
				_backList[i] = [];
				_frontList[i] = [];
				for(var j:int=0; j<=_mapHeight/_tileHeight; j++){
					_frontList[i][j] = [];
				}
			}
			for(var i:int=0; i<res.backList.length; i++){
				var px:int = res.backList[i].x / _tileWidth;
				var py:int = res.backList[i].y / _tileHeight;
				_backList[px][py] = res.backList[i].url;
			}


			while(_backLayer.numChildren > 0){
				var mt:MapTile = _backLayer.removeChildAt(0) as MapTile;
			}
			_tiles = [];
			for(var i:int=0; i<_w; i++){
				_tiles[i] = [];
				for(var j:int=0; j<_h; j++){
					_tiles[i][j] = new MapTile;
					(_tiles[i][j] as MapTile).x = i * _tileWidth;
					(_tiles[i][j] as MapTile).y = j * _tileHeight;
					_backLayer.addChild(_tiles[i][j]);
				}
			}
			
			_items = [];
			for(var i:int=0; i<res.frontList.length; i++){
				var x0:int = res.frontList[i].x / _tileWidth;
				var x1:int = Math.ceil(res.frontList[i].x + res.frontList[i].width) / _tileWidth;
				var y0:int = res.frontList[i].y / _tileHeight;
				var y1:int = Math.ceil(res.frontList[i].y + res.frontList[i].height) / _tileHeight;
				for(var j:int=x0; j<=x1; j++){
					if(!_items[j]){
						_items[j] = [];
					}
					for(var k:int=y0; k<=y1; k++){
						if(!_items[j][k]){
							_items[j][k] = []
						}
						(_frontList[j][k] as Array).push(res.frontList[i].url);
						var mt:MapTile = new MapTile;
						mt.x = res.frontList[i].x;
						mt.y = res.frontList[i].y;
						(_items[j][k] as Array).push(mt);
					}
				}
			}			
			while(_frontLayer.numChildren > 0){
				var mt:MapTile = _frontLayer.removeChildAt(0) as MapTile;
			}
			
			
		}
		
		public function setCenter(px:int, py:int):void{
			var x0:int = Math.floor((px - stage.width/2) / _tileWidth);
			var x1:int = Math.floor((px + stage.width/2) / _tileWidth);
			var y0:int = Math.floor((py - stage.height/2) / _tileHeight);
			var y1:int = Math.floor((py + stage.height/2) / _tileHeight);
			_frontLayer.x = stage.width / 2 - px;
			_frontLayer.y = stage.height / 2 - py;
			_backLayer.x = stage.width / 2 - (px - x0 * _tileWidth);
			_backLayer.y = stage.height / 2 - (py - y0 * _tileHeight);
			if(x0 < 0){
				x0 = 0;
				x1 = x0 + _w - 1;
				_frontLayer.x = 0;
				_backLayer.x = 0;
			}
			if(x1 > _mapWidth / _tileWidth){
				x1 = _mapWidth / _tileWidth;
				x0 = x1 - _w + 1;
				_frontLayer.x = stage.width - int(_mapWidth / _tileWidth + 1) * _tileWidth;
				_backLayer.x = stage.width - _w * _tileWidth;
			}
			if(y0 < 0){
				y0 = 0;
				y1 = y1 + _h - 1;
				_frontLayer.y = 0;
				_backLayer.y = 0;
			}
			if(y1 > _mapHeight / _tileHeight){
				y1 = _mapHeight / _tileHeight;
				y0 = y1 - _h + 1;
				_frontLayer.y = stage.height - int(_mapHeight / _tileHeight + 1) * _tileHeight;
				_backLayer.y = stage.height - _h * _tileHeight;
			}
			
			if(x1 > x0 + _w - 1){
				x1 = x0 + _w - 1
			}
			
			if(y1 > y0 + _h - 1){
				y1 = y0 + _h - 1
			}
			// 要把不在屏幕中显示的移除掉
			for(var i:int=0; i<_frontLayer.numChildren; i++){
				
			}
			
			for(var i:int=x0; i<= x1; i++){
				for(var j:int=y0; j<=y1; j++){
					var mt:MapTile = _tiles[i-x0][j-y0] as MapTile;
					mt.url = _url + _backList[i][j];
				}
			}
			
			trace(_frontLayer.x, _frontLayer.y, "|", _backLayer.x, _backLayer.y, "|", x0, x1,"|", y0, y1,"|", px, py);
		}
		
		public function getMousePoint():Point{
			return new Point(_frontLayer.mouseX, _frontLayer.mouseY);
		}
	}
}